package com.laboratory.servicecustomer.workBench.controller;

import com.Laboratory.util.timeUtil;
import com.Laboratory.workBench.pojo.equipment;
import com.Laboratory.workBench.pojo.equipmentRepair;
import com.Laboratory.workBench.service.equipmentService;
import com.laboratory.servicecustomer.constant.constantArgs;
import com.laboratory.servicecustomer.util.fileUtil;
import com.laboratory.servicecustomer.utilPojo.comm;
import javafx.scene.chart.ValueAxis;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author acok
 * @description:TODO
 * @Date 2022/12/18 20:46
 * @Version 1.0
 */
@Controller
@RequestMapping("/equipment")
public class equipmentController {
    @DubboReference(interfaceClass = equipmentService.class,version = "1.0.0")
    private equipmentService equipmentService;


    @RequiresRoles(value = {"users","admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"users:equipmentSearch","admin:equipmentSearch","Root:highest"},logical = Logical.OR)
    @PostMapping("/getEquipmentListByPage")
    @ResponseBody
    public Object getEquipmentListByRoomNo(@RequestParam(value = "currentRoom") String roomNo,
                                           @RequestParam(value = "currentPage") Integer pageNo,
                                           @RequestParam(value = "sizePage") Integer pageSize){
        comm comm=new comm();
        Map<String,Object> map=new HashMap<>();
        pageNo=(pageNo-1)*pageSize;
        map.put("roomNo",roomNo);
        map.put("pageNo",pageNo);
        map.put("pageSize",pageSize);
        try{
            List<equipment> equipmentList = equipmentService.getEquipmentListByPage(map);
            int rows = equipmentService.getEquipmentRowsByPage(roomNo);
            Map<String,Object> resultMap=new HashMap<>();
            resultMap.put("list",equipmentList);
            resultMap.put("rows",rows);
            comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
            comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            comm.setOther(resultMap);
        }catch (Exception e){
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }

    @RequiresRoles(value = {"users","admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"users:equipmentRepair","admin:equipmentRepair","Root:highest"},logical = Logical.OR)
    @GetMapping("/getEquipmentRepairList")
    @ResponseBody
    public Object getEquipmentRepairList(){
        comm comm=new comm();
        try{
            List<equipmentRepair> repairList = equipmentService.getEquipmentRepair();
            comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
            comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            comm.setOther(repairList);
        }catch(Exception e){
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }

    @RequiresRoles(value = {"users","admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"users:equipmentRepair","admin:equipmentRepair","Root:highest"},logical = Logical.OR)
    @PostMapping("/insertRepairReport")
    @ResponseBody
    public Object insertRepairReport(@RequestParam(value = "repairId") String repairId,
                                     @RequestParam(value = "equipmentNo") String equipmentNo,
                                     @RequestParam(value = "equipmentName") String equipmentName,
                                     @RequestParam(value = "repairCause") String repairCause,
                                     @RequestParam(value = "roomId") String roomId){
        comm comm=new comm();
        try{
            Map<String,Object> paramMap=new HashMap<>();
            paramMap.put("repairId",repairId);
            paramMap.put("equipmentNo",equipmentNo);
            paramMap.put("equipmentName",equipmentName);
            paramMap.put("repairCause",repairCause);
            paramMap.put("roomId",roomId);
            paramMap.put("createTime",timeUtil.getNowTime(new Date()));

            if (equipmentService.insertEquipmentRepairRecord(paramMap)==2){
                comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
                comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            }else {
                comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
                comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
            }
        }
        catch (Exception e){
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }

    @RequiresRoles(value = {"admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"admin:equipmentRepair","Root:highest"},logical = Logical.OR)
    @RequestMapping("/deleteRepairRecordByIds")
    @ResponseBody
    public Object deleteRepairRecordByIds(String []ids){
     comm comm=new comm();
     try{
         int i = equipmentService.deleteRepairRecordByIds(ids);
         System.out.println(i);
         if (i>0){
             comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
             comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
         }else {
             comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
             comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
         }
     }catch (Exception e){
         e.printStackTrace();
         comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
         comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
     }
     return comm;
    }

    @RequiresRoles(value = {"users","admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"users:equipmentRepair","admin:equipmentRepair","Root:highest"},logical = Logical.OR)
    @RequestMapping("/downFileByRepairRecord")
    public void downLoadFileByRepairRecord(HttpServletResponse response){
        HSSFWorkbook wb=null;
        OutputStream outWeb=null;
        try {
            List<equipmentRepair> repairList = equipmentService.getEquipmentRepair();
            if(repairList!=null&&repairList.size()>0) {
                /*写入服务器*/
                 wb= new HSSFWorkbook();
                //创建数据表
                HSSFSheet sheet = wb.createSheet(constantArgs.FAIL_DOWNLOAD_NAME);
                //创建首行
                HSSFRow row = sheet.createRow(0);
                //创建首行首单元格
                HSSFCell cell = row.createCell(0);
                cell.setCellValue("记录主键");
                cell = row.createCell(1);
                cell.setCellValue("报修原因");
                cell = row.createCell(2);
                cell.setCellValue("报修日期");
                cell = row.createCell(3);
                cell.setCellValue("设备主键");
                cell = row.createCell(4);
                cell.setCellValue("设备编号");
                cell = row.createCell(5);
                cell.setCellValue("设备名");
                cell = row.createCell(6);
                cell.setCellValue("所属房间");
                cell =row.createCell(7);
                cell.setCellValue("房间主键");

                //将List逐行写入数据表
                int i = 1;
                for (equipmentRepair equipmentRepair :
                        repairList) {
                    row = sheet.createRow(i++);
                    cell = row.createCell(0);
                    cell.setCellValue(equipmentRepair.getRepairId());
                    cell = row.createCell(1);
                    cell.setCellValue(equipmentRepair.getRepairCause());
                    cell = row.createCell(2);
                    cell.setCellValue(equipmentRepair.getCreateTime());
                    cell = row.createCell(3);
                    cell.setCellValue(equipmentRepair.getEquipmentId());
                    cell = row.createCell(4);
                    cell.setCellValue(equipmentRepair.getEquipmentNo());
                    cell = row.createCell(5);
                    cell.setCellValue(equipmentRepair.getEquipmentName());
                    cell = row.createCell(6);
                    cell.setCellValue(equipmentRepair.getRoomNo());
                    cell =row.createCell(7);
                    cell.setCellValue(equipmentRepair.getRoomId());
                }
                /*下载至浏览器*/
                response.setContentType("application/octet-stream;charset=UTF-8");
                response.addHeader("Content-Disposition", "attachment;filename=Statistics.xls");
                outWeb = response.getOutputStream();

                wb.write(outWeb);
                wb.close();
                outWeb.flush();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            fileUtil.closeFileStream(outWeb,wb);
        }
    }
}
